package com.android.lovely.ui.dialog

import androidx.core.graphics.toColorInt
import androidx.core.text.buildSpannedString
import androidx.core.text.color
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.android.lovely.R
import com.android.lovely.databinding.DialogChatRegenerateBinding
import com.android.lovely.framework.BaseDialogFragment
import com.android.lovely.manager.KVStore
import com.android.lovely.room.chat.database.ReceivedTextMessage
import com.android.lovely.ui.chat.ChatMessageActivity
import com.android.lovely.ui.chat.ConversationViewModel
import com.android.lovely.ui.chat.getChatId
import com.android.lovely.ui.viewmodel.ChatViewModel
import com.kotlin.extensions.view.ViewExt.clickable

/**
 *
 * @author why
 * @date 2025/3/28 15:51
 */
class ChatRegenerateDialog : BaseDialogFragment<DialogChatRegenerateBinding>() {
    private val viewModel by activityViewModels<ConversationViewModel> {
        viewModelFactory {
            initializer {
                ConversationViewModel(getChatId())
            }
        }
    }

    override fun initializeViews() {
        val message: ReceivedTextMessage = arguments?.getParcelable("message") as? ReceivedTextMessage  ?: return
        binding.tvTip.isSelected = true
        binding.title.text = String.format(
            getString(R.string.regenerate_cost),
            viewModel.character.info.point.toString()
        )
        binding.tvTip.clickable {
            it.isSelected = it.isSelected.not()
        }
        binding.cancel.clickable {
            if (binding.tvTip.isSelected) {
                KVStore.regenerateRemind = false
            }
            dismissAllowingStateLoss()
        }
        binding.confirm.clickable {
            if (binding.tvTip.isSelected) {
                KVStore.regenerateRemind = false
            }
            message?.let {
                viewModel.refreshMessageContent(it)
            }
            dismissAllowingStateLoss()
        }
    }
}